package com.lib.framework.base.mvvm

import android.app.Application
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.lib.framework.entity.Event
import com.lib.framework.entity.StartActivityInfo
import com.lib.framework.manager.thread.mainThreadRun

/**
 * 基本的 ViewModel
 *
 * @author keanbin
 */
abstract class BaseViewModel(application: Application) : AndroidViewModel(application) {

    companion object {

        const val KEY_TEXT = "text"

        /**
         * 公共的事件标记范围 99900 ~ 99999
         */
        const val EVENT_FINISH = 99900 // 关闭页面事件
        const val EVENT_PROMPT_TEXT = 99901 // 显示提示文本对话框事件
        const val EVENT_SHOW_LOADING_DIALOG = 99902 // 显示Loading对话框事件
        const val EVENT_CLOSE_LOADING_DIALOG = 99903 // 关闭Loading对话框事件
        const val EVENT_START_ACTIVITY = 99904 // 启动页面事件
        const val EVENT_SET_RESULT_OK = 99905 // 设置 RESULT_OK
    }


    protected var mTag = this.javaClass.canonicalName ?: "BaseViewModel"

    val mContext: Context = application

    /**
     * 事件 LiveData
     */
    protected val mEventLiveData = MutableLiveData<Event>()

    /**
     * 获取 EventLiveData
     */
    fun getEventLiveData() = mEventLiveData

    /**
     * 发送 事件
     */
    fun sendEvent(eventCode: Int, extraObj: Any? = null, extras: Bundle = Bundle()) {
        mainThreadRun {
            mEventLiveData.value = Event(eventCode, extras, extraObj)
        }
    }

    /**
     * 显示提示
     */
    fun prompt(msg: String) {
        sendEvent(EVENT_PROMPT_TEXT, msg)
    }

    /**
     * 显示提示
     */
    fun promptResId(resid: Int) {
        prompt(mContext.getString(resid))
    }

    /**
     * 显示 Loading Dialog
     *
     * @param isCancelable  用户是否可以主动取消对话框
     */
    fun showLoadingDialog(isCancelable: Boolean = true, text: String? = null) {
        val builder = Bundle()
        if (text != null) {
            builder.putString(KEY_TEXT, text)
        }
        sendEvent(EVENT_SHOW_LOADING_DIALOG, isCancelable, builder)
    }

    /**
     * 关闭 Loading Dialog
     */
    fun closeLoadingDialog() {
        sendEvent(EVENT_CLOSE_LOADING_DIALOG)
    }

    /**
     * 启动页面， 如果设置requestCode ，就是调用 startActivityForResult
     */
    fun startActivity(
        cls: Class<*>, finishActivity: Boolean = false, bundle: Bundle? = null,
        requestCode: Int? = null, isSetResultOk: Boolean = false
    ) {
        val intent = Intent(mContext, cls)
        if (bundle != null) {
            intent.putExtras(bundle)
        }

        sendEvent(
            EVENT_START_ACTIVITY,
            StartActivityInfo(intent, finishActivity, requestCode, isSetResultOk)
        )
    }

    /**
     * 关闭页面
     */
    fun finish() {
        sendEvent(EVENT_FINISH)
    }

    /**
     * 设置 ResultOK
     */
    fun setResultOK(intent: Intent? = null) {
        sendEvent(EVENT_SET_RESULT_OK, extraObj = intent)
    }

    /**
     * 页面回调
     */
    open fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

    }
}